package org.hefan.sysmanager.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.PageList;
import org.hefan.sysmanager.domain.SystemDictionaryDetail;
import org.hefan.sysmanager.query.SystemDictionaryDetailQuery;
import org.hefan.sysmanager.service.ISystemDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionary/detail")
@Api(tags = "数据字典详细信息入口", description = "数据字典详细信息入口描述")
public class SystemDictionaryDetailController {


    /**
     * 注入systemDictionaryDetailService
     */
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 查询全部数据字典类型
     * @return List<SystemDictionaryDetail>
     */
    @GetMapping
    @ApiOperation(value = "查询全部数据字典类型", notes = "")
    public List<SystemDictionaryDetail> findAll() {
        return systemDictionaryDetailService.loadAll();
    }

    /**
     * 查询单个SystemDictionaryDetail数据字典类型对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个数据字典类型",notes = "需要传入id")
    public SystemDictionaryDetail findOne(@PathVariable("id")Long id) {
        return systemDictionaryDetailService.loadById(id);
    }

    /**
     * 分页查询
     * @param systemDictionaryDetailQuery 分页查询参数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "通过分页查询数据字典类型",notes = "需要传入分页数据")
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery) {
        return systemDictionaryDetailService.queryPage(systemDictionaryDetailQuery);
    }

    /**
     * 删除数据字典类型
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据字典类型",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            systemDictionaryDetailService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 添加或修改数据字典类型
     * @param systemDictionaryDetail
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改数据字典类型",notes = "有id则为修改,没有id则是添加")
    public AjaxResult saveOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        try {
            if (systemDictionaryDetail.getId() == null) {
                systemDictionaryDetailService.save(systemDictionaryDetail);
            } else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除数据字典
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除数据字典",notes = "传需要批量删除的数据字典id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            systemDictionaryDetailService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
}
